{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3ca1b3da",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d6544dd6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 0, 9, 5, 1],\n",
       "       [9, 3, 1, 2, 7],\n",
       "       [1, 6, 5, 6, 4]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(0,10,size=(3,5))\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "45b063ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 3., nan,  9.,  5.,  1.],\n",
       "       [ 9.,  3.,  1.,  2.,  7.],\n",
       "       [ 1.,  6.,  5.,  6.,  4.]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = data.astype(float)\n",
    "data[0,1] = np.nan\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6e571303",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\winlab\\AppData\\Local\\Temp/ipykernel_14892/1771165735.py:1: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  data/0\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[inf, nan, inf, inf, inf],\n",
       "       [inf, inf, inf, inf, inf],\n",
       "       [inf, inf, inf, inf, inf]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data/0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "dbb97c41",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.NAN == np.NAN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2f0db54a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "nan"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[0,1]*2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8b8e6c65",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 3., nan,  9.,  5.,  1.],\n",
       "       [ 9.,  3., nan,  2.,  7.],\n",
       "       [ 1.,  6.,  5.,  6.,  4.]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[1,2] = np.NAN\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a0d0e240",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3., 9., 5., 1., 9., 3., 2., 7., 1., 6., 5., 6., 4.])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[~np.isnan(data)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "319c8ff1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 6., 5., 6., 4.]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lines = np.where(np.isnan(data))[0]\n",
    "np.delete(data,lines,axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "9289964e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['59' '89']\n",
      " ['90' '32']\n",
      " ['78' '45.5']\n",
      " ['34' '']\n",
      " ['' '56']\n",
      " ['23' '56']]\n",
      "[[59.  89. ]\n",
      " [90.  32. ]\n",
      " [78.  45.5]\n",
      " [34.   nan]\n",
      " [ nan 56. ]\n",
      " [23.  56. ]]\n",
      "[[59.  89. ]\n",
      " [90.  32. ]\n",
      " [78.  45.5]\n",
      " [34.   0. ]\n",
      " [ 0.  56. ]\n",
      " [23.  56. ]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([148. , 122. , 123.5,  34. ,  56. ,  79. ])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores = np.loadtxt(\"nan_scores.csv\",delimiter=',',skiprows=1,encoding=\"utf-8\",dtype=str)\n",
    "print(scores)\n",
    "scores[scores==\"\"] = np.NAN\n",
    "scores1 = scores.astype(float)\n",
    "print(scores1)\n",
    "scores1[np.isnan(scores1)] = 0\n",
    "print(scores1)\n",
    "# 除了delete用axis=0表示行以外，其他的大部分函数都是axis=1表示行\n",
    "scores1.sum(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "224ec226",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[59.  89. ]\n",
      " [90.  32. ]\n",
      " [78.  45.5]\n",
      " [34.   nan]\n",
      " [ nan 56. ]\n",
      " [23.  56. ]]\n",
      "(6, 2)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[59. , 89. ],\n",
       "       [90. , 32. ],\n",
       "       [78. , 45.5],\n",
       "       [34. , 55.7],\n",
       "       [56.8, 56. ],\n",
       "       [23. , 56. ]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores2 = scores.astype(float)\n",
    "print(scores2)\n",
    "print(scores2.shape)\n",
    "for x in range(scores2.shape[1]):\n",
    "    col = scores2[:,x]\n",
    "    non_nan_col = col[~np.isnan(col)]\n",
    "    mean = non_nan_col.mean()\n",
    "    col[np.isnan(col)] = mean\n",
    "scores2"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
